clear
est clear


	
	use "$input/full_geoid_pair_panel.dta"
	
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	preserve
		
		bysort to_geoid: egen total_in=sum(migration)
		rename to_geoid fips
		keep fips total_in
		duplicates drop fips, force
		save "$input/temp1.dta", replace
	restore
		
		bysort from_geoid: egen total_out=sum(migration)
		rename from_geoid fips	
		
		duplicates drop fips, force
		
		cap drop _merge
		keep fips total_out
		merge 1:1 fips using "$input/temp1.dta"
			drop _merge
		
		g net=total_in-total_out
		
		replace total_in=total_in/100000
		replace total_out=total_out/100000
		replace net=net/100000
		g ratio=total_in/total_out
		
		
		export delimited "$figdat/total_net_migration.csv", replace
		
		
	